<html>
<head>
    <#assign basePath=request.contextPath>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>后台管理系统</title>
    <#include "/WEB-INF/views/template/bootMainCss.ftl" parse=true encoding="utf-8">
    <link href="${basePath}/bootstrap/plugin/icheck/flat/blue.css" rel="stylesheet">
    <link href="${basePath}/bootstrap/css/good.lzy.css" rel="stylesheet">
    <style>
        .col-xs-3 {
            margin-bottom: 10px;
        }
        #button {
            width: 94%;
            position:absolute;
            bottom:10px;
            left: 3%;
        }
    </style>
</head>
<body>
<div class="panel-body">
    <div class="row">
        <div class="col-lg-12">
            <div class="row" id="main">

            </div>
        </div>
    </div>

    <button type="button" class="btn btn-primary btn-lg btn-block" id="button" onclick="saveUserRole();">保存修改</button>
</div>
</div>

<div class="good-add-suc">
    <span>
        <b>编辑成功</b>
        该页面将在 5 秒后自动跳转！
        <a href="${basePath}/item/page">返回列表</a>
    </span>
</div>

<#include "/WEB-INF/views/template/bootMainJs.ftl" parse=true encoding="utf-8">
<script src="${basePath}/bootstrap/plugin/icheck/icheck.js"></script>
<script src="${basePath}/bootstrap/plugin/layer/layer.js" type="text/javascript"></script>
<script type="application/javascript">
    $(function () {
        $.ajax({
            type: 'GET',
            dataType: "json",
            url: '${basePath}/admin/manager/role/json?userId=' + ${userId},
            success: function (data) {
                var template = '';
                $('#main').html('');
                for(var i = 0; i < data.length; i++){
                    template += '<div class="col-xs-3">';
                    if (data[i].checked == 1) {
                        template += '<input type="checkbox" name="role" id="' + data[i].id + '" checked>    ' + data[i].rlname;
                    } else {
                        template += '<input type="checkbox" name="role" id="' + data[i].id + '">    ' + data[i].rlname;
                    }
                    template += '</div>';
                }
                $('#main').html(template);

                $('input').iCheck({
                    checkboxClass: 'icheckbox_flat-blue'
                });
            }
        });
    });
    
    function saveUserRole() {
        var roleIds = [];
        $('input:checkbox[name=role]:checked').each(function(i){
            roleIds.push($(this).attr('id'));
        });
        $.ajax({
            url: '${basePath}/admin/manager/${userId}/updateUserRole',
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            data:JSON.stringify(roleIds),
            success: function (data) {
                console.log(data);
                if (data.code=='0'||data.code == '0000') {
                    closePage();
                    layer.open({
                        type: 1,
                        title: false,
                        content: $(".good-add-suc"),
                        area: ['600px', '300px'],
                        time: 5000,
                        end: function(){
                            parent.layer.closeAll();
                            window.location.href = '${basePath}/admin/manager';
                        }
                    });
                } else {
                    layer.alert(data.desc, {
                        icon : 5
                    });
                }
            }
        });
    }


    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    //关闭窗口并刷新页面
    function closePage() {
        parent.layer.close(index);
    }

</script>
</body>
</html>